//
//  Shader.fsh
//  GLKViewSnapshot
//
//  Created by Nishiyama Nobuyuki on 2013/09/10.
//  Copyright (c) 2013年 Nishiyama Nobuyuki. All rights reserved.
//

precision mediump float;

varying lowp vec2 v_textureCoord;
varying lowp vec4 v_color;

uniform sampler2D u_SamplerBG;
uniform sampler2D u_SamplerY;
uniform sampler2D u_SamplerUV;

uniform bool u_IsVideo;
uniform bool u_IsImage;
uniform bool u_IsColor;

uniform mat3 u_colorConversionMatrix;

void main() {
    
    if (u_IsVideo) {
        
        mediump vec3 yuv;
        lowp vec3 rgb;
        
        yuv.x = (texture2D(u_SamplerY, v_textureCoord).r - (16.0/255.0))* 1.0;
        yuv.yz = (texture2D(u_SamplerUV, v_textureCoord).rg - vec2(0.5, 0.5))* 1.0;
        
        rgb = u_colorConversionMatrix * yuv;
        
        gl_FragColor = vec4(rgb,1);
    } else if (u_IsImage){
        
        gl_FragColor = texture2D(u_SamplerBG, v_textureCoord);
    } else if (u_IsColor) {
        
        gl_FragColor = v_color;
    }
}
